<?php
/**
 * 房源模型
 */

namespace app\official\model;


use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Err;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

class House extends BaseModel {

    //拍卖阶段
    const TYPE_1 = 1;
    const TYPE_2 = 2;
    const TYPE_3 = 3;
    const TYPE_4 = 4;
    const TYPE_5 = 5;
    const TYPE_6 = 6;

    const TYPE_CONFIG = [
        self::TYPE_1 => '一拍流拍',
        self::TYPE_2 => '二拍流拍',
        self::TYPE_3 => '三拍流拍',
        self::TYPE_4 => '四拍流拍',
        self::TYPE_5 => '五拍流拍',
        self::TYPE_6 => '六拍流拍',
    ];

    const BED_ROOM_COMFIG = [
        0 => '未知',
        1 => '一居室',
        2 => '二居室',
        3 => '三居室',
        4 => '四居室',
    ];

    /**
     * 首页房源
     * @param $params
     * @param $data
     * @param $pageNum
     * @param $count
     * @param $page
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function IndexHouseList($params, &$data, &$pageNum, &$count, &$page) {
        $map[] = ['h.is_shelf', '=', 1];
        $map[] = ['h.deleted', '=', 0];
        $region_data = Db::name('region')->where('city_code', $params['city_code'])->find();
        $codes = $region_data['p_codes'] . $params['city_code'] . ',';
        $map[] = ['h.city_codes', 'like', trim($codes) . '%'];

        if (!empty($params['type'])) {
            if ($params['type'] == 'new') {//今日房源
                $now_time = date('Y-m-d', time());
                $now_time1 = strtotime($now_time);
                $map[] = ['ha.day_itime', '=', $now_time1];
            }
            if ($params['type'] == 'good') {//热门房源
                $map[] = ['h.important', '=', 1];
                $map[] = ['ha.auction_status', '=', 1];
            }
        }

        $orderby = ['h.important' => 'desc', 'ha.auction_status' => 'asc', 'ha.start_time' => 'desc', 'h.id' => 'desc'];

        $page = intval($params['page']);
        $long = intval($params['long']);

        $column = 'h.cover,h.title,ha.market_price,h.id,ha.access_num,ha.auction_status,ha.end_time,ha.type,IF(ha.type=4,ha.pay_selloff,ha.initial_price) as initial_price,h.bedroom,h.livingroom,hv.name as vname';


        $map[] = ['ha.type', 'in', [1, 2, 3, 4, 5, 6]];
        self::houseData($orderby, $map, '', $page, $long, $column, $data, $pageNum, $count);

        if ($params['type'] == 'good' && count($data) < $long) {//热门房源
            $long1 = $long - count($data);
            $map1[] = ['h.city_codes', 'like', trim($codes) . '%'];
            $map1[] = ['h.important', '=', 0];
            $map1[] = ['ha.auction_status', '=', 1];
            $map1[] = ['h.is_shelf', '=', 1];
            $map1[] = ['ha.type', 'in', [1, 2, 3, 4, 5, 6]];
            self::houseData($orderby, $map1, '', $page, $long1, $column, $data2, $pageNum, $count);
            $data = array_merge($data, $data2);
        }

        foreach ($data as $k => $v) {
            deal_money_list($data[$k]['initial_price'], $initial_price, $initial_price_unit);
            $data[$k]['initial_price'] = $initial_price;
            $data[$k]['initial_price_unit'] = $initial_price_unit;

            deal_money_list($data[$k]['market_price'], $market_price, $market_price_unit);
            $data[$k]['consult_price'] = $market_price;
            $data[$k]['consult_price_unit'] = $market_price_unit;

            unset($data[$k]['type']);

            if ($data[$k]['bedroom'] > 0 && $data[$k]['livingroom'] > 0) {
                $data[$k]['title'] = $data[$k]['vname'] . ' ' . $data[$k]['bedroom'] . '室' . $data[$k]['livingroom'] . '厅';
            } else {
                $data[$k]['title'] = $data[$k]['vname'];
            }

            $data[$k]['end_time'] = empty($data[$k]['end_time']) ? '' : date('m月d日 H:i', $v['end_time']);
        }
    }

    /**
     * 房源列表
     * @param $params
     * @param $page
     * @param $pageNum
     * @param $count
     * @param $data
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function houseList($params, &$page, &$pageNum, &$count, &$data) {
        $column = "h.id,h.cover,h.build_area,ht.name as tname,ha.type,ha.discount,hv.name as vname,ha.access_num,ha.end_time,ha.market_price,ha.auction_status,IF(ha.type=4,ha.pay_selloff,ha.initial_price) as initial_price,h.bedroom,h.livingroom,h.area,ba.name as bname";
        $whereRaw = '';
        $map[] = ['h.is_shelf', '=', 1];
        $map[] = ['h.deleted', '=', 0];
        //关键字搜索
        if (!empty($params['key_word'])) {
            $map[] = ['hv.name|h.title', 'like', '%' . trim($params['key_word']) . '%'];
        }

        //城市或者区域
        $city_code = $params['city_code'];
        $region_data = Db::name('region')->where('city_code', $city_code)->find();
        $codes = $region_data['p_codes'] . $city_code . ',';
        $map[] = ['h.city_codes', 'like', trim($codes) . '%'];

        //地铁找房
        if (!empty($params['lng']) && !empty($params['lat'])) {
            $str = 'ST_Distance_Sphere(POINT(' . $params['lng'] . ',' . $params['lat'] . '),hv.location)';
            $column .= ",{$str} as jl";
            $map['jl'] = 1;
        }

        //价格区间
        if (!empty($params['price_interval'])) {
            $map[] = createSectionQuery1($params['price_interval'], 'ha.initial_price');
        }

        //折扣比
        if (!empty($params['discount_interval'])) {
            $whereRaw .= createSectionDiscount($params['discount_interval'], 'ha.discount');
        }

        //拍卖状态
        if (!empty($params['a_status'])) {
            $auction_status = explode(',', $params['a_status']);
            $map[] = ['ha.auction_status', 'in', $auction_status];
        }
        //拍卖阶段
        if (!empty($params['a_type'])) {
            $au_type = explode(',', $params['a_type']);
            $map[] = ['ha.type', 'in', $au_type];
        }

        //排序
        if (!empty($params['orderby'])) {
            //默认排序
            if ($params['orderby'] == 1) {
                $orderby = ['ha.auction_status' => 'asc', 'ha.start_time' => 'asc', 'initial_price' => 'asc', 'ha.discount' => 'asc'];
            }
            //最新发布
            if ($params['orderby'] == 2) {
                $orderby = ['h.on_time' => 'desc', 'ha.auction_status' => 'asc', 'ha.start_time' => 'asc', 'initial_price' => 'asc', 'ha.discount' => 'asc'];
            }
            //价格高低
            if ($params['orderby'] == 3) {
                $orderby = ['initial_price' => 'desc', 'ha.auction_status' => 'asc', 'ha.start_time' => 'asc', 'ha.discount' => 'asc'];
            }
            //价格低高
            if ($params['orderby'] == 4) {
                $orderby = ['initial_price' => 'asc', 'ha.auction_status' => 'asc', 'ha.start_time' => 'asc', 'ha.discount' => 'asc'];
            }
            //折扣高低
            if ($params['orderby'] == 5) {
                $orderby = ['ha.discount' => 'desc', 'ha.auction_status' => 'asc', 'ha.start_time' => 'asc', 'initial_price' => 'asc'];
            }
            //折扣低高
            if ($params['orderby'] == 6) {
                $orderby = ['ha.discount' => 'asc', 'ha.auction_status' => 'asc', 'ha.start_time' => 'asc', 'initial_price' => 'asc'];
            }
            //开拍时间远近
            if ($params['orderby'] == 7) {
                $orderby = ['ha.start_time' => 'desc', 'ha.auction_status' => 'asc', 'initial_price' => 'asc', 'ha.discount' => 'asc'];
            }
            //开拍时间近远
            if ($params['orderby'] == 8) {
                $orderby = ['ha.start_time' => 'asc', 'ha.auction_status' => 'asc', 'initial_price' => 'asc', 'ha.discount' => 'asc'];
            }
        } else {
            $orderby = ['ha.auction_status' => 'asc', 'ha.start_time' => 'asc', 'initial_price' => 'asc', 'ha.discount' => 'asc'];
        }

        empty($params['page']) ? $page = 1 : $page = intval($params['page']);
        empty($params['long']) ? $long = 10 : $long = intval($params['long']);

        $map[] = ['ha.type', 'in', [1, 2, 3, 4, 5, 6]];
        self::houseData($orderby, $map, $whereRaw, $page, $long, $column, $data, $pageNum, $count);

        foreach ($data as $k => $v) {

            if ($data[$k]['bedroom'] > 0 && $data[$k]['livingroom'] > 0) {
                $data[$k]['vname'] = $data[$k]['vname'] . ' ' . $data[$k]['bedroom'] . '室' . $data[$k]['livingroom'] . '厅';
            }

            unset($data[$k]['bedroom']);
            unset($data[$k]['livingroom']);

            $data[$k]['bname'] = $data[$k]['bname'] ?? '';

            if ($data[$k]['discount'] <= 1 && $data[$k]['discount'] != -1) {
                $data[$k]['discount'] = '低于1折';
            } elseif ($data[$k]['discount'] == -1) {
                $data[$k]['discount'] = '- -';
            } else {
                $data[$k]['discount'] = $v['discount'];
            }

            deal_money_list($data[$k]['initial_price'], $initial_price, $initial_price_unit);
            $data[$k]['initial_price'] = $initial_price;
            $data[$k]['initial_price_unit'] = $initial_price_unit;

            deal_money_list($data[$k]['market_price'], $market_price, $market_price_unit);
            $data[$k]['consult_price'] = $market_price;
            $data[$k]['consult_price_unit'] = $market_price_unit;

            $data[$k]['build_area'] = deal_area_list($data[$k]['build_area'], 4);

            $data[$k]['end_time'] = empty($data[$k]['end_time']) ? '' : date('Y/m/d H:i', $v['end_time']);
        }
    }

    /**
     * 房源sql
     * @param $orderby
     * @param $map
     * @param $page
     * @param $long
     * @param $data
     * @param $pageNum
     * @param $count
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    private static function houseData($orderby, $map, $whereRaw, $page, $long, $column, &$data, &$pageNum, &$count) {
        $map_ = $map;
        $stl = db('house')
            ->alias('h')
            ->leftJoin('house_auction ha', 'ha.id=h.auction_id')
            ->leftJoin('house_type ht', 'ht.id=h.house_type_id')
            ->leftJoin('house_village hv', 'hv.id=h.village_id')
            ->leftJoin('business_area ba', 'ba.id=h.business_area_id')
            ->field($column);
        if (isset($map_['jl'])) {
            $stl->having("jl <3000");
            unset($map_['jl']);
        }
        if (!empty($whereRaw)) {
            $stl->whereRaw($whereRaw);
        }
        $count = $stl
            ->where($map_)
            ->select();
        $count = count($count);

        $sql = db('house')
            ->alias('h')
            ->leftJoin('house_auction ha', 'ha.id=h.auction_id')
            ->leftJoin('house_type ht', 'ht.id=h.house_type_id')
            ->leftJoin('house_village hv', 'hv.id=h.village_id')
            ->leftJoin('business_area ba', 'ba.id=h.business_area_id')
            ->field($column)
            ->order($orderby)
            ->page($page, $long);
        if (isset($map['jl'])) {
            $sql->having("jl <3000");
            unset($map['jl']);
        }
        if (!empty($whereRaw)) {
            $sql->whereRaw($whereRaw);
        }
        $data = $sql
            ->where($map)
            ->select();
        if ($long > 0) {
            $pageNum = ceil($count / $long);
        } else {
            $pageNum = 1;
        }
    }

    /**
     * 房源详情
     * @param $params
     * @param $data
     * @param $history
     * @param $theSameArea
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getInfo($params, &$data, &$history, &$success_history) {
        $data = db('house')
            ->alias('h')
            ->leftJoin('house_auction ha', 'ha.id=h.auction_id')
            ->leftJoin('house_type ht', 'h.house_type_id = ht.id')
            ->leftJoin('house_village hv', 'h.village_id = hv.id')
            ->leftJoin('business_area ba', 'h.business_area_id = ba.id')
            ->field('h.title,h.build_area,ha.start_time,ha.end_time,h.pic,ha.enroll_num,ha.access_num,ha.remind_num,ha.type,ha.discount,ht.name as tname,ha.consult_price,ha.market_price,ha.bond,h.floor,hv.name as vname,h.address,ha.price_lower_offset,h.build_time,h.renovation_type,h.bedroom,h.livingroom,x(hv.location) as lng,Y(hv.location) as lat,IF(ha.type=4,ha.pay_selloff,ha.initial_price) as initial_price,ba.name as bname,h.area,ha.final_price')
            ->where('h.id', $params['id'])
            ->where('h.deleted', 0)
            ->find();
        if (empty($data)) {
            $data = [];
            Err::add('房源不存在', RespCode::DATA_ERR);
            return;
        }

        if ($data['discount'] <= 1 && $data['discount'] != -1) {
            $data['discount'] = '低于1折';
        } elseif ($data['discount'] == -1) {
            $data['discount'] = '- -';
        } else {
            $data['discount'] = (string)$data['discount'];
        }

        deal_money_info($data['consult_price'], $consult_price, $consult_price_unit);
        $data['consult_price'] = $consult_price;
        $data['consult_price_unit'] = $consult_price_unit;

        deal_money_info($data['market_price'], $market_price, $market_price_unit);
        $data['market_price'] = $market_price;
        $data['market_price_unit'] = $market_price_unit;

        deal_money_info($data['initial_price'], $initial_price, $initial_price_unit);
        $data['initial_price'] = $initial_price;
        $data['initial_price_unit'] = $initial_price_unit;

        deal_money_info($data['final_price'], $final_price, $final_price_unit);
        $data['final_price'] = $final_price;
        $data['final_price_unit'] = $final_price_unit;

        $data['bond'] = deal_data_info($data['bond'], 2);

        $data['price_lower_offset'] = deal_data_info($data['price_lower_offset'], 2);

        if (!empty($data['build_area']) && !empty($data['initial_price'])) {
            if (empty($data['initial_price_unit'])) {
                $data['danjia'] = (string)($data['initial_price'] / ($data['build_area'] / 10000));
            } else {
                $data['danjia'] = (string)($data['initial_price'] * 100000 / ($data['build_area'] / 1000));
            }
        } else {
            $data['danjia'] = '';
        }

        if ($data['bedroom'] > 0 && $data['livingroom'] > 0) {
            $data['bedroom'] = $data['bedroom'] . '室' . $data['livingroom'] . '厅';
            unset($data['livingroom']);
        } else {
            $data['bedroom'] = '';
            unset($data['livingroom']);
        }

        $data['vname'] = empty($data['vname']) ? '' : $data['vname'] . ' ' . $data['bedroom'];

        $data['build_area'] = deal_data_info($data['build_area'], 4);

        $data['bname'] = $data['bname'] ?? '';

        if (!empty($data['start_time'])) {
            $data['start_time'] = date('Y/m/d H:i', $data['start_time']);
        } else {
            $data['start_time'] = '';
        }
        if (!empty($data['end_time'])) {
            $data['end_time'] = date('Y/m/d H:i', $data['end_time']);
        } else {
            $data['end_time'] = '';
        }
        if (!empty($data['pic'])) {
            $data['pic'] = explode(',', $data['pic']);
        } else {
            $data['pic'] = [];
        }
        $data['lng'] = !empty($data['lng']) ? (string)$data['lng'] : '';
        $data['lat'] = !empty($data['lat']) ? (string)$data['lat'] : '';

        //经纪人信息
        $agentinfo = db('company_agent')
            ->where('company_id', 2)
            ->where('is_customer', 1)
            ->field('cover,name,phone')
            ->find();
        $data['agent_cover'] = $agentinfo['cover'];
        $data['agent_name'] = $agentinfo['name'];
        $data['agent_phone'] = $agentinfo['phone'];
        //历史拍卖
        $history = self::getHistory($params['id'], $data['type']);
        //小区历史成交
        $village_id = Db::name('house')->where('id', $params['id'])->value("village_id");
        $success_history = self::villageSuccessHistory($village_id);
    }

    /**
     * 小区成交
     * @param $village_id
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function villageSuccessHistory($village_id) {
        if ($village_id) {
            $success_history = Db::name("house")
                ->alias('h')
                ->leftJoin('house_auction ha', "h.auction_id=ha.id")
                ->where('h.village_id', $village_id)
                ->where('h.village_id', '<>', 0)
                ->where('ha.auction_status', 3)
                ->where('ha.type', "<>", 6)
                ->field('h.title,ha.type,ha.discount,h.build_area,ha.final_price,h.cover')
                ->order('ha.utime', 'desc')
                ->order('ha.itime', 'desc')
                ->select();
            foreach ($success_history as $k1 => $v1) {
                if (!empty($success_history[$k1]['build_area']) && !empty($success_history[$k1]['final_price'])) {
                    $success_history[$k1]['danjia'] = (string)($success_history[$k1]['final_price'] / $success_history[$k1]['build_area'] * 100);
                } else {
                    $success_history[$k1]['danjia'] = '';
                }
                $success_history[$k1]['build_area'] = deal_area_list($success_history[$k1]['build_area'], 4);
                deal_money_list($success_history[$k1]['final_price'], $final_price, $final_price_unit);
                $success_history[$k1]['final_price'] = $final_price;
                $success_history[$k1]['final_price_unit'] = $final_price_unit;
            }
        } else {
            $success_history = [];
        }
        return $success_history;
    }

    /**
     * 拍卖历史
     * @param $house_id
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getHistory($house_id, $type) {
        $data = Db::name('house_auction')
            ->where('house_id', $house_id)
            ->where('type', '<>', $type)
            ->field('type,start_time,end_time,IF(type=4,pay_selloff,initial_price) as initial_price')
            ->order('type', 'asc')
            ->select();
        $type_config = self::TYPE_CONFIG;
        foreach ($data as $k => $v) {
            $data[$k]['typename'] = $type_config[$v['type']];

            deal_money_list($data[$k]['initial_price'], $initial_price, $initial_price_unit);
            $data[$k]['initial_price'] = $initial_price;
            $data[$k]['initial_price_unit'] = $initial_price_unit;

            $data[$k]['start_time'] = !empty($v['start_time']) ? date('Y年m月d日', $v['start_time']) : '';
            $data[$k]['end_time'] = !empty($v['end_time']) ? date('Y年m月d日', $v['end_time']) : '';
            unset($data[$k]['type']);
        }
        return $data;
    }

    /**
     * 名称搜索房源
     * @param $params
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function searchHouse($params) {
        $city_code = $params['city_code'];
        $region_data = Db::name('region')->where('city_code', $city_code)->find();
        $codes = $region_data['p_codes'] . $city_code . ',';
        $data = db('house')
            ->field('title,id')
            ->where('title', 'like', '%' . $params['title'] . '%')
            ->where('city_codes', 'like', trim($codes) . '%')
            ->where('is_shelf', 1)
            ->limit(0, 10)
            ->select();
        if (empty($data)) {
            return [];
        } else {
            return $data;
        }
    }

    /**
     * 地图找房-房源列表-条件
     * @param $params
     * @param $data
     * @param $count
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function houseMapList($params, &$data, $type) {
        $page = empty($params['page']) ? 1 : intval($params['page']);
        $long = empty($params['long']) ? 10 : intval($params['long']);
        $column = "h.id,h.title,ha.type,x(hv.location) as lng,Y(hv.location) as lat,h.cover,IF(ha.type=4,ha.pay_selloff,ha.initial_price) as initial_price,h.build_area,h.livingroom,h.bedroom,hv.name as vname";
        $whereRaw = '';
        $map = [];
        $map[] = ['h.is_shelf', '=', 1];
        $map[] = ['h.deleted', '=', 0];
        $map[] = ['ha.auction_status', 'in', [1, 2]];
        //附近房源
        if (!empty($params['id'])) {
            $map[] = ['h.id', '<>', $params['id']];
        }
        $city_code = $params['city_code'];
        //默认当前区域!
        $region_data = Db::name('region')->where('city_code', $city_code)->find();
        $codes = $region_data['p_codes'] . $city_code . ',';
        $map[] = ['h.city_codes', 'like', trim($codes) . '%'];

        //价格区间
        if (!empty($params['price_interval'])) {
            $map[] = createSectionQuery1($params['price_interval'], 'ha.initial_price');
        }

        //面积区间
        if (!empty($params['build_area_interval'])) {
            $map[] = createSectionQuery1($params['build_area_interval'], 'h.build_area');
        }

        //拍卖状态
        if (!empty($params['a_status'])) {
            $auction_status = explode(',', $params['a_status']);
            $map[] = ['ha.auction_status', 'in', $auction_status];
        }

        //折扣比
        if (!empty($params['discount_interval'])) {
            $whereRaw .= createSectionDiscount($params['discount_interval'], 'ha.discount');
        }

        //拍卖阶段
        if (!empty($params['a_type'])) {
            $au_type = explode(',', $params['a_type']);
            $map[] = ['ha.type', 'in', $au_type];
        }

        //经纬度
        if (!empty($params['lng']) && !empty($params['lat'])) {
            $str = 'ST_Distance_Sphere(POINT(' . $params['lng'] . ',' . $params['lat'] . '),hv.location)';
            $column .= ",{$str} as jl";
            $map['jl'] = 1;
        }

        if ($type == 2) {
            //3公里附近的房源,用于地图渲染
            $data1 = db('house')
                ->alias('h')
                ->leftJoin('house_auction ha', 'ha.id = h.auction_id')
                ->leftJoin('house_village hv', 'hv.id = h.village_id')
                ->field($column)
                ->order('jl', 'asc');
            if (isset($map['jl'])) {
                $data1->having("jl <3000");
                unset($map['jl']);
            }
            $data1 = $data1->where($map)
                ->select();

            foreach ($data1 as $k1 => $v1) $data1[$k1]['lng-lat'] = $v1['lng'] . '-' . $v1['lat'];
            //给重复数据附加新键
            $data1 = array_field_repeat($data1, 'lng-lat');
            //根据某个键，去掉重复数据
            $data1 = array_repeat($data1, 'lng-lat', true);

            foreach ($data1 as $k2 => $v2) {
                if ($data1[$k2]['bedroom'] > 0 && $data1[$k2]['livingroom'] > 0) {
                    $data1[$k2]['title'] = $data1[$k2]['vname'] . ' ' . $data1[$k2]['bedroom'] . '室' . $data1[$k2]['livingroom'] . '厅';
                } else {
                    $data1[$k2]['title'] = $data1[$k2]['vname'];
                }
                unset($data1[$k2]['bedroom']);
                unset($data1[$k2]['livingroom']);
                $data1[$k2]['cover'] = empty($data1[$k2]['cover']) ? config('global.empty_house_img') : $data1[$k2]['cover'];
                deal_money_list($data1[$k2]['initial_price'], $initial_price, $initial_price_unit);
                $data1[$k2]['initial_price'] = $initial_price;
                $data1[$k2]['initial_price_unit'] = $initial_price_unit;
                if (isset($data1[$k2]['build_area'])) {
                    $data1[$k2]['build_area'] = deal_area_list($data1[$k2]['build_area'], 4);
                }
                unset($data1[$k2]['type']);
                $data1[$k2]['jl'] = (intval($data1[$k2]['jl']) / 1000) . '公里';
                //过滤没有经纬度的房源
                if (empty($data1[$k2]['lng']) || empty($data1[$k2]['lat'])) {
                    unset($data1[$k2]);
                } else {
                    $data1[$k2]['lng'] = (string)$data1[$k2]['lng'];
                    $data1[$k2]['lat'] = (string)$data1[$k2]['lat'];
                }
            }
            $data['data1'] = array_merge($data1);
        }

        if ($type == 1) {
            //跟距离无关的房源,只跟条件有关，由远到近
            unset($map['jl']);
            $count = db('house')
                ->alias('h')
                ->leftJoin('house_auction ha', 'ha.id = h.auction_id')
                ->leftJoin('house_village hv', 'hv.id = h.village_id')
                ->where($map);
            if (!empty($whereRaw)) {
                $count->whereRaw($whereRaw);
            }
            $count = $count->count();
            if ($count == 0) {
                $page = 1;
                $pageNum = 1;
                $data2 = [];
            } else {
                $data2 = db('house')
                    ->alias('h')
                    ->leftJoin('house_auction ha', 'ha.id = h.auction_id')
                    ->leftJoin('house_village hv', 'hv.id = h.village_id')
                    ->field($column)
                    ->page($page, $long)
                    ->order('jl asc')
                    ->where($map);
                if (!empty($whereRaw)) {
                    $data2->whereRaw($whereRaw);
                }
                $data2 = $data2
                    ->order('jl', 'asc')
                    ->select();
                foreach ($data2 as $k3 => $v3) {
                    if ($data2[$k3]['bedroom'] > 0 && $data2[$k3]['livingroom'] > 0) {
                        $data2[$k3]['title'] = $data2[$k3]['vname'] . ' ' . $data2[$k3]['bedroom'] . '室' . $data2[$k3]['livingroom'] . '厅';
                    } else {
                        $data2[$k3]['title'] = $data2[$k3]['vname'];
                    }
                    unset($data2[$k3]['bedroom']);
                    unset($data2[$k3]['livingroom']);
                    $data2[$k3]['cover'] = empty($data2[$k3]['cover']) ? config('global.empty_house_img') : $data2[$k3]['cover'];
                    deal_money_list($data2[$k3]['initial_price'], $initial_price2, $initial_price_unit2);
                    $data2[$k3]['initial_price'] = $initial_price2;
                    $data2[$k3]['initial_price_unit'] = $initial_price_unit2;
                    if (isset($data2[$k3]['build_area'])) {
                        $data2[$k3]['build_area'] = deal_area_list($data2[$k3]['build_area'], 4);
                    }
                    unset($data2[$k3]['type']);
                    $data2[$k3]['jl'] = (intval($data2[$k3]['jl']) / 1000) . '公里';
                    //过滤没有经纬度的房源
                    if (empty($data2[$k3]['lng']) || empty($data2[$k3]['lat'])) {
                        unset($data2[$k3]);
                    } else {
                        $data2[$k3]['lng'] = (string)$data2[$k3]['lng'];
                        $data2[$k3]['lat'] = (string)$data2[$k3]['lat'];
                    }
                }
                $pageNum = ceil($count / $long);
            }
            $data['data2']['count'] = $count;
            $data['data2']['page'] = $page;
            $data['data2']['pageNum'] = $pageNum;
            $data['data2']['list'] = array_merge($data2);
        }
    }

}
